﻿#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "dbmanager.h"
#include "socketmanager.h"
#include <QQmlContext>
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QTime>

void logOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg){

    QString txt;
    QString time = QDate::currentDate().toString("yyyy/MM/dd/") + QTime::currentTime().toString("hh:mm:ss");

    txt = QString("%1>>%2").arg(time, qFormatLogMessage(type, context, msg));

    QString strPath = QCoreApplication::applicationDirPath() + "/log.txt";
    QFile outFile(strPath);
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream out(&outFile);
    out << txt << Qt::endl;

}


int main(int argc, char *argv[])
{

    qInstallMessageHandler(logOutput);

    QGuiApplication app(argc, argv);
    DBManager dbmanager;
    SocketManager socketmanager;

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("dbManager", &dbmanager);
    engine.rootContext()->setContextProperty("socketManager", &socketmanager);

    const QUrl url("qrc:/main.qml");
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

